//test123
//test1234

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;

namespace Tile
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        //The texture of the sprite and the a variable to hold it's position
        Texture2D square, background;
        Vector2 squarePosition;
        
        //The integer coordinates of the 'tile' the mouse is in
        int TileX;
        int TileY;
       
        //Predefined height and width of the sprite/tile
        int tileWidth = 40;
        int tileHeight = 40;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            //Make the mouse visible
            this.IsMouseVisible = true;
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.ApplyChanges();
            //Load the sprite texture from the Content
            squarePosition = new Vector2(0, 0);
            square = this.Content.Load<Texture2D>("square");
            background = this.Content.Load<Texture2D>("bg");
            
        }
        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            //Grab the mouse state
            MouseState mouse = Mouse.GetState();

            //Calculate X and Y coordinates
            TileX = (int)Math.Floor((float)(mouse.X / tileWidth));
            TileY = (int)Math.Floor((float)(mouse.Y / tileHeight));

            //Set the square to a whole tile coordinate
            squarePosition.X = (TileX * tileWidth)+(tileWidth/2);
            squarePosition.Y = (TileY * tileHeight)+(tileHeight/2);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
                //Draw the sprite from the center
            spriteBatch.Begin();
            spriteBatch.Draw(square, squarePosition, null, Color.White, 0, new Vector2(square.Width / 2, square.Height / 2), 1, SpriteEffects.None, 0);
            spriteBatch.Draw(background, new Vector2(0,0), Color.White);
            
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
